package com.tencent.timsdk.model.info;

import android.util.Log;

import com.tencent.TIMConversation;
import com.tencent.TIMConversationType;
import com.tencent.TIMFriendshipManager;
import com.tencent.TIMGroupDetailInfo;
import com.tencent.TIMGroupManager;
import com.tencent.TIMMessage;
import com.tencent.TIMUserProfile;
import com.tencent.TIMValueCallBack;
import com.tencent.timsdk.model.Message;
import com.tencent.timsdk.model.MessageFactory;
import com.tencent.timsdk.model.TextMessage;

import java.util.Collections;
import java.util.List;

/**
 * 好友或群聊的会话
 */
public class NormalConversation extends Conversation {

    public static final int NAME = 1;
    public static final int FACE = 2;
    public static final int MESSAGE = 3;

    //最后一条消息
    private Message lastMessage;

    public NormalConversation(TIMConversation conversation) {
        this.conversation = conversation;
        type = conversation.getType();
        identify = conversation.getPeer();
        conversation.getMessage(1, null, new TIMValueCallBack<List<TIMMessage>>() {
            @Override
            public void onError(int i, String s) {
                Log.e("NormalConversation", "get message error" + s);
            }

            @Override
            public void onSuccess(List<TIMMessage> timMessages) {
                if (timMessages != null && !timMessages.isEmpty()) {
                    Message message = MessageFactory.getMessage(timMessages.get(0));
                    setLastMessage(message);
                }
            }
        });
        if (type == TIMConversationType.Group) {
            TIMGroupManager.getInstance().getGroupPublicInfo(Collections.singletonList(identify), new TIMValueCallBack<List<TIMGroupDetailInfo>>() {
                @Override
                public void onSuccess(List<TIMGroupDetailInfo> timGroupDetailInfos) {
                    if (timGroupDetailInfos != null && !timGroupDetailInfos.isEmpty()) {
                        TIMGroupDetailInfo timGroupDetailInfo = timGroupDetailInfos.get(0);
                        faceUrl = timGroupDetailInfo.getFaceUrl();
                        name = timGroupDetailInfo.getGroupName();
                    }
                }

                @Override
                public void onError(int i, String s) {
                }

            });
        } else if (type == TIMConversationType.C2C) {
            TIMFriendshipManager.getInstance().searchFriend(identify, new TIMValueCallBack<TIMUserProfile>() {
                @Override
                public void onError(int i, String s) {
                }

                @Override
                public void onSuccess(TIMUserProfile timUserProfile) {
                    if (timUserProfile != null) {
                        faceUrl = timUserProfile.getFaceUrl();
                        name = timUserProfile.getNickName();
                    }
                }
            });
        }
    }


    public void setLastMessage(Message lastMessage) {
        this.lastMessage = lastMessage;
    }


    @Override
    public String getAvatar() {
        return faceUrl;
    }

    /**
     * 获取最后一条消息摘要
     */
    @Override
    public String getLastMessageSummary() {
        if (conversation.hasDraft()) {
            TextMessage textMessage = new TextMessage(conversation.getDraft());
            if (lastMessage == null || lastMessage.getMessage().timestamp() < conversation.getDraft().getTimestamp()) {
                return "草稿" + textMessage.getSummary();
            } else {
                return lastMessage.getSummary();
            }
        } else {
            if (lastMessage == null) return "";
            return lastMessage.getSummary();
        }
    }


    /**
     * 获取未读消息数量
     */
    @Override
    public long getUnreadNum() {
        if (conversation == null) return 0;
        return conversation.getUnreadMessageNum();
    }

    /**
     * 将所有消息标记为已读
     */
    @Override
    public void readAllMessage() {
        if (conversation != null) {
            conversation.setReadMessage();
        }
    }

    @Override
    public String getName() {
        return name;
    }


    /**
     * 获取最后一条消息的时间
     */
    @Override
    public long getLastMessageTime() {
        if (conversation.hasDraft()) {
            if (lastMessage == null || lastMessage.getMessage().timestamp() < conversation.getDraft().getTimestamp()) {
                return conversation.getDraft().getTimestamp();
            } else {
                return lastMessage.getMessage().timestamp();
            }
        }
        if (lastMessage == null) return 0;
        return lastMessage.getMessage().timestamp();
    }
}
